//
//  CustomView.m
//  CAShapeLayer
//
//  Created by TengFeiLi on 15/7/14.
//  Copyright (c) 2015年 TengFeiLi. All rights reserved.
//

#import "CircleView.h"
#import "POP.h"

@implementation CircleView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self.layer setCornerRadius:25];
        [self.layer setBackgroundColor:[UIColor colorWithRed:0.16 green:0.72 blue:1.0 alpha:1.0].CGColor];
    }
    return self;
}

-(void)progress:(CGPoint)point{
    

}
-(void)loading{
    
}
-(void)reset{
    [self.layer pop_removeAllAnimations];
    for (CAShapeLayer *layer in self.layer.sublayers) {
        [layer removeFromSuperlayer];
    }
    
    self.layer.opacity = 0.0;
    self.layer.opacity = 1.0;
    self.layer.transform = CATransform3DIdentity;
    [self.layer setMasksToBounds:YES];
    [self.layer setBackgroundColor:[UIColor colorWithRed:0.16 green:0.72 blue:1.0 alpha:1.0].CGColor];
    [self.layer setCornerRadius:25.0f];
    [self setBounds:CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];
    self.layer.position = CGPointMake(self.center.x, 180.0);
}

-(void)startAnimate{
    
    [self pop_removeAllAnimations];
    
    //Config progress line
    CAShapeLayer *progressLayer = [CAShapeLayer layer];
    progressLayer.strokeColor = [UIColor colorWithWhite:1.0 alpha:0.98].CGColor;
    progressLayer.lineCap   = kCALineCapRound;
    progressLayer.lineJoin  = kCALineJoinBevel;
    progressLayer.lineWidth = 26.0;
    progressLayer.strokeEnd = 0.0;
    
    UIBezierPath *progressline = [UIBezierPath bezierPath];
    [progressline moveToPoint:CGPointMake(25.0, 25.0)];
    [progressline addLineToPoint:CGPointMake(CGRectGetWidth([[UIScreen mainScreen]bounds]) - 25, 25.0)];
    
    NSLog(@"%@",NSStringFromCGRect(self.bounds));
    
    progressLayer.path = progressline.CGPath;
    
    [self.layer addSublayer:progressLayer];
    
    POPSpringAnimation * springScale = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    springScale.springBounciness = 5;
    springScale.springBounciness = 12;
    springScale.toValue = [NSValue valueWithCGPoint:CGPointMake(1, 1)];
    
    POPSpringAnimation * boundsAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBounds];
    boundsAnim.springBounciness = 10;
    boundsAnim.springSpeed = 6;
    boundsAnim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen]bounds]), 50)];
    
    boundsAnim.completionBlock =  ^(POPAnimation * anim,BOOL finished){
        if(finished){
            UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
            POPBasicAnimation * progressBoundsAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPShapeLayerStrokeEnd];
            progressBoundsAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
            progressBoundsAnim.duration = 1.0;
            progressBoundsAnim.fromValue = @0.0;
            progressBoundsAnim.toValue = @1.0;
            
            [progressLayer pop_addAnimation:progressBoundsAnim forKey:@"AnimateBounds"];
            progressBoundsAnim.completionBlock = ^(POPAnimation *anim, BOOL finished) {
                if (finished) {
                    UIGraphicsEndImageContext();
                }
            };
            
        }
    };
    
    [self.layer pop_addAnimation:springScale forKey:@"scale"];
    [self.layer pop_addAnimation:boundsAnim forKey:@"bounds"];
    
    
}

@end
